

#include "board.h"
#include "aero_conf.h"


extern __IO uint16_t CCR1_Val;
extern __IO uint16_t CCR2_Val;
extern __IO uint16_t CCR3_Val;
extern __IO uint16_t CCR4_Val;

/*------------------------------------------------------------------------------
VERSION
------------------------------------------------------------------------------*/   
#define __AERO_BSP_VERSION_MAIN   (0x01) /*!< [31:24] main version */
#define __AERO_BSP_VERSION_SUB1   (0x01) /*!< [23:16] sub1 version */
#define __AERO_BSP_VERSION_SUB2   (0x00) /*!< [15:8]  sub2 version */
#define __AERO_BSP_VERSION_RC     (0x00) /*!< [7:0]  release candidate */
#define __AERO_BSP_VERSION        ((__AERO_BSP_VERSION_MAIN << 24)\
                                            |(__AERO_BSP_VERSION_SUB1 << 16)\
                                            |(__AERO_BSP_VERSION_SUB2 << 8 )\
                                            |(__AERO_BSP_VERSION_RC))

/**
  * @brief  This method returns the  BSP Driver revision
  * @retval version : 0xXYZR (8bits for each decimal, R for RC)
  */
uint32_t aeroBsp_GetVersion(void)
{
  return __AERO_BSP_VERSION;
}

/*------------------------------------------------------------------------------
RCC设置
STM32的PB3、PB4，分别是JTAG的JTDO和NJTRST引脚，在没关闭JTAG功能之前，在程序中是配置不了这些引脚的功能的。
要配置这些引脚，首先要开启AFIO时钟，然后在AFIO的设置中，释放这些引脚

PB3，PB4，PA13，PA14，PA15是特殊的IO口，用作JTAG/SWD仿真器的调试接口

重映射的方式一共有三种。分别描述如下：
1.GPIO_Remap_SWJ_JTAGDisable： !< JTAG-DP Disabled and SW-DP Enabled  即能用PB3，PB4，PA15做普通IO，PA13&14用于SWD调试
2.GPIO_Remap_SWJ_Disable：  !< Full SWJ Disabled (JTAG-DP + SW-DP)   5个引脚全为普通引脚，但不能再用JTAG&SWD仿真器调试，只能用st-link调试 
3.GPIO_Remap_SWJ_NoJTRST： !< Full SWJ Enabled (JTAG-DP + SW-DP) but without JTRST PB4可为普通IO口，JTAG&SWD正常使用，但JTAG没有复位
如果你用到所有的五个引脚当做普通IO口，那么上述步骤二中的重映射配置应写为GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable, ENABLE); 
如果你用PB3，PB4，PA15做普通IO，PA13&14用于SWD调试，则重映射配置应写为GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE); 
同理可配置只用PB4可为普通IO口的情况。

------------------------------------------------------------------------------*/   
void aero_InitRcc(void)
{
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);  
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);
  
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
  

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
  GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);//禁止JTAG功能  
//  GPIO_PinRemapConfig(GPIO_FullRemap_TIM2, ENABLE);  
  
      /* TIM2 clock enable */


}

void aero_Initp10(void)
{
    GPIO_InitTypeDef        GPIO_InitStructure;
    
  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_10;  
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;           // 复用推挽输出  
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
  GPIO_Init(GPIOB, &GPIO_InitStructure);  
  
}

/*------------------------------------------------------------------------------
UART调试
------------------------------------------------------------------------------*/   
void aeroUartGpioConfig(void)
{
  GPIO_InitTypeDef        GPIO_InitStructure;
  //配置UART_ZHU TXpin
  GPIO_InitStructure.GPIO_Pin = UART_DBG_TX_PIN;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(UART_DBG_PORT, &GPIO_InitStructure);

  //UART_ZHU RXpin
  GPIO_InitStructure.GPIO_Pin = UART_DBG_RX_PIN;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(UART_DBG_PORT, &GPIO_InitStructure);    
}

void aeroUartModeConfig(void)
{
  USART_InitTypeDef USART_InitStructure;
  
  /* USARTy and USARTz configured as follow:
        - BaudRate = 230400 baud  
        - Word Length = 8 Bits
        - One Stop Bit
        - Even parity
        - Hardware flow control disabled (RTS and CTS signals)
        - Receive and transmit enabled
  */
  USART_InitStructure.USART_BaudRate = 115200;
  USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  USART_InitStructure.USART_StopBits = USART_StopBits_1;
  USART_InitStructure.USART_Parity = USART_Parity_No;
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  
  /* Configure USARTy */
  USART_Init(UART_DBG, &USART_InitStructure);
  USART_Cmd(UART_DBG,ENABLE);
}

void aero_InitUart(void)
{
  aeroUartGpioConfig();
  aeroUartModeConfig();
}

/*------------------------------------------------------------------------------
MCOPIN设置为8M时钟
------------------------------------------------------------------------------*/   

void aeroBsp_InitMco(void)
{
  GPIO_InitTypeDef      GPIO_InitStructure;
  
//  RCC_DeInit();//一定需要
    
  GPIO_InitStructure.GPIO_Pin = MCO_PIN;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(MCO_PORT, &GPIO_InitStructure);
  
  RCC_MCOConfig(RCC_MCO_HSE);//内部高速时钟
  
}


/*------------------------------------------------------------------------------
ADC设置
------------------------------------------------------------------------------*/   
void aeroAdc1GpioConfig()
{
  GPIO_InitTypeDef        GPIO_InitStructure;
  /* ADC Channel configuration */

  /* Configure ADC Channel7 as analog input */
  GPIO_InitStructure.GPIO_Pin = ADC_PIN ;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  GPIO_Init(ADC_PORT, &GPIO_InitStructure);
  
}

void aeroAdc1ModeConfig(void)
{
//  RCC_ADCCLKConfig(RCC_PCLK2_Div6);
//  ADC_DeInit(ADC1);
  ADC_InitTypeDef ADC_InitStructure;

  ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
  ADC_InitStructure.ADC_ScanConvMode = DISABLE;
  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStructure.ADC_NbrOfChannel = 1;
  ADC_Init(ADC1, &ADC_InitStructure);

  /* ADC1 regular channel14 configuration */ 
  ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 1, ADC_SampleTime_1Cycles5);

  
  /* Enable ADC1 */
  ADC_Cmd(ADC1, ENABLE);

  /* Enable ADC1 reset calibration register */   
  ADC_ResetCalibration(ADC1);
  /* Check the end of ADC1 reset calibration register */
  while(ADC_GetResetCalibrationStatus(ADC1));

  /* Start ADC1 calibration */
  ADC_StartCalibration(ADC1);
  /* Check the end of ADC1 calibration */
  while(ADC_GetCalibrationStatus(ADC1));
     
  /* Start ADC1 Software Conversion */ 
  ADC_SoftwareStartConvCmd(ADC1, ENABLE);
  
}

void aero_InitAdc1(void)
{
  aeroAdc1GpioConfig();
  aeroAdc1ModeConfig();
}

/*------------------------------------------------------------------------------
设置板上LED
------------------------------------------------------------------------------*/   

void aero_InitLed(void)
{
  GPIO_InitTypeDef        GPIO_InitStructure;

  GPIO_InitStructure.GPIO_Pin = LED;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  
  GPIO_Init(LED_PORT, &GPIO_InitStructure);

}

/*------------------------------------------------------------------------------
设置接收VT PIN
因为常低，有效时变为高电平瞬态，因此为输入拉低设置
------------------------------------------------------------------------------*/   

void aero_InitVt(void)
{
  GPIO_InitTypeDef        GPIO_InitStructure;

  GPIO_InitStructure.GPIO_Pin = VT1_PIN | VT2_PIN;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; 
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  
  GPIO_Init(VT1_PORT, &GPIO_InitStructure);
}

/*------------------------------------------------------------------------------
读取VT的状态
------------------------------------------------------------------------------*/   
uint16_t aero_ReadVt(void)
{
  uint16_t data;
  data = (GPIO_ReadInputData(VT1_PORT)& 0x0003);
  
  return data;
}

/*------------------------------------------------------------------------------
初始化OLED
对于PB4PB3的
------------------------------------------------------------------------------*/   
void aero_InitOled(void)
{
  GPIO_InitTypeDef        GPIO_InitStructure;

  GPIO_InitStructure.GPIO_Pin = OLEDCS | OLEDDC | OLEDMOSI  |OLEDCLK | OLEDRES;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  
  GPIO_Init(OLED_PORT, &GPIO_InitStructure);
  

}

/*------------------------------------------------------------------------------
初始化TIM2 控制发射端TE
------------------------------------------------------------------------------*/
void aeroTim2GpioConfig()
{
  GPIO_InitTypeDef GPIO_InitStructure;

  /* GPIOC Configuration:Pin6, 7, 8 and 9 as alternate function push-pull */
  GPIO_InitStructure.GPIO_Pin = TE_PIN;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_Init(TE_PORT, &GPIO_InitStructure);

}

/*
//采用内部时钟给TIM2提供时钟源

       TIM_InternalClockConfig(TIM2);

       //预分频系数为36000-1，这样计数器时钟为72MHz/36000 = 2kHz

       TIM_TimeBaseStructure.TIM_Prescaler = 36000 - 1;

       //设置时钟分割

       TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;

       //设置计数器模式为向上计数模式

       TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

       //设置计数溢出大小，每计2000个数就产生一个更新事件

       TIM_TimeBaseStructure.TIM_Period = 2000 - 1;

       //将配置应用到TIM2中

       TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);

 

       //清除溢出中断标志

       TIM_ClearFlag(TIM2, TIM_FLAG_Update);

       //禁止ARR预装载缓冲器

       TIM_ARRPreloadConfig(TIM2, DISABLE);

       //开启TIM2的中断

       TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);


  当前仅仅注册了溢出中断
*/
void aeroTim2ModeConfig()
{
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  TIM_OCInitTypeDef  TIM_OCInitStructure;
  
  TIM_DeInit( TIM2);//复位TIM2定时器

  /* ---------------------------------------------------------------
    TIM2 Configuration: Output Compare Timing Mode:
    TIM2 counter clock at 6 MHz
    CC1 update rate = TIM2 counter clock / CCR1_Val = 146.48 Hz
    CC2 update rate = TIM2 counter clock / CCR2_Val = 219.7 Hz
    CC3 update rate = TIM2 counter clock / CCR3_Val = 439.4 Hz
    CC4 update rate = TIM2 counter clock / CCR4_Val = 878.9 Hz
  --------------------------------------------------------------- */

  /* Compute the prescaler value */
    // 为了提高38K精度，不进行分配既 counter使用72M工作
  TIM_TimeBaseStructure.TIM_Prescaler = 10;// 预分频 不分频直接使用72M工作  
  TIM_TimeBaseStructure.TIM_Period = 1894; // 72M/(1894+1) = 37.995K 比较接近
  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;       //不分频
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;   //计数方式 up
  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);               //初始化
//  TIM_ClearFlag(TIM2,TIM_FLAG_Update);  //清除标志
//  TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
  
  /* Output Compare Timing Mode configuration: Channel1 */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing;
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

//  TIM_OC1Init(TIM2, &TIM_OCInitStructure);

//  TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Disable);

  /* Output Compare Timing Mode configuration: Channel2 */
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = CCR2_Val;

//  TIM_OC2Init(TIM2, &TIM_OCInitStructure);

//  TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Disable);

  /* Output Compare Timing Mode configuration: Channel3 */
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = 947;

  TIM_OC3Init(TIM2, &TIM_OCInitStructure);

  TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Enable);

  /* Output Compare Timing Mode configuration: Channel4 */
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = 500;

  TIM_OC4Init(TIM2, &TIM_OCInitStructure);

  TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Enable);

  //xxg
  /* TIM IT enable */
  //TIM_ITConfig(TIM2, TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3 | TIM_IT_CC4, ENABLE);
  TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
  TIM_ARRPreloadConfig(TIM2, ENABLE);

  /* TIM2 enable counter */
  TIM_Cmd(TIM2, ENABLE);
  
}

void aero_InitTe(void)
{
    aeroTim2GpioConfig();
    aeroTim2ModeConfig();
}


void aero_InitPTaddr(void)
{
  GPIO_InitTypeDef        GPIO_InitStructure;

  GPIO_InitStructure.GPIO_Pin = PT_A0;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  
  GPIO_Init(PT_PORT, &GPIO_InitStructure);

}

void aeroTim3ModeConfig(void)
{
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  
  TIM_DeInit( TIM3);//复位TIM2定时器  
  //xxg 假设需要一个20us的信号，既50K
  //72M/50K  = 1440 既Prescaler为 1440-1 = 1439;
  //实际测试 VT为 40ms高电平，因此对计数器加1000*20us = 20ms取中间值
  TIM_TimeBaseStructure.TIM_Prescaler = 1439;
  TIM_TimeBaseStructure.TIM_Period = 8999; // 定时器周期数 base0   9 = 5K 99 = 500hz 999 = 50hz 9999 = 5hz
  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;       //不分频
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;   //计数方式 up
  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);               //初始化
  TIM_ClearFlag(TIM3,TIM_FLAG_Update);  //清除标志
  TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);

}

void aero_Tim3Enable(void)
{
    TIM_ClearFlag(TIM3,TIM_FLAG_Update);  //清除标志
    TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
    TIM_Cmd(TIM3, ENABLE);
}

void aero_Tim3Disable(void)
{
    TIM_ClearFlag(TIM3,TIM_FLAG_Update);  //清除标志
    TIM_ITConfig(TIM3,TIM_IT_Update,DISABLE);
    TIM_Cmd(TIM3, DISABLE);  
}